/*
 * EcmaScriptMidlet.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.scriptme.ecmascript.data.ESValue;
import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Interpreter;

/**
 * The Class EcmaScriptMidlet.
 * 
 * @author Cesar
 */
public class EcmaScriptMidlet extends MIDlet implements CommandListener {

	/**
	 * Creates a new instance of EcmaScriptMidlet.
	 */
	public EcmaScriptMidlet() {
		initialize();
	}

	/** The text value. */
	private TextBox textValue;// GEN-BEGIN:MVDFields

	/** The ok command1. */
	private Command okCommand1;

	/** The exit command1. */
	private Command exitCommand1;

	/** The form1. */
	private Form form1;

	/** The es source. */
	private TextField esSource;

	/** The ok command2. */
	private Command okCommand2;

	/** The exit command2. */
	private Command exitCommand2;// GEN-END:MVDFields

	// GEN-LINE:MVDMethods

	/**
	 * Called by the system to indicate that a command has been invoked on a
	 * particular displayable.//GEN-BEGIN:MVDCABegin
	 * 
	 * @param command
	 *            the Command that ws invoked
	 * @param displayable
	 *            the Displayable on which the command was invoked
	 */
	public void commandAction(Command command, Displayable displayable) {// GEN-END:MVDCABegin
		// Insert global pre-action code here
		if (displayable == textValue) {// GEN-BEGIN:MVDCABody
			if (command == exitCommand1) {// GEN-END:MVDCABody

				// Do nothing//GEN-LINE:MVDCAAction7
				// Insert post-action code here
			} else if (command == okCommand1) {// GEN-LINE:MVDCACase7

				// Do nothing//GEN-LINE:MVDCAAction5
				// Insert post-action code here
			}// GEN-BEGIN:MVDCACase5
		} else if (displayable == form1) {
			if (command == exitCommand2) {// GEN-END:MVDCACase5
				this.notifyDestroyed();
				// Do nothing//GEN-LINE:MVDCAAction13
				// Insert post-action code here
			} else if (command == okCommand2) {// GEN-LINE:MVDCACase13
				try {
					Interpreter i = new Interpreter(this);
					try {
						i.reset();
						i.toggleDebugParse();
					} catch (EcmaScriptException ex) {
						ex.printStackTrace();
					}
					// i.executeString(esSource.getString(), "source");
					ESValue val = i.evaluate(esSource.getString(), true);
					System.out.println(val.toString());
					// Do nothing
					// Insert post-action code here
				} catch (EcmaScriptException ex) {
					ex.printStackTrace();
				}

				// Do nothing//GEN-LINE:MVDCAAction11
				// Insert post-action code here
			}// GEN-BEGIN:MVDCACase11
		}// GEN-END:MVDCACase11
		// Insert global post-action code here
	}// GEN-LINE:MVDCAEnd

	/**
	 * This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
	 */
	private void initialize() {// GEN-END:MVDInitBegin
		// Insert pre-init code here
		getDisplay().setCurrent(get_form1());// GEN-LINE:MVDInitInit
		// Insert post-init code here
	}// GEN-LINE:MVDInitEnd

	/**
	 * This method should return an instance of the display.
	 * 
	 * @return the display
	 */
	public Display getDisplay() {// GEN-FIRST:MVDGetDisplay
		return Display.getDisplay(this);
	}// GEN-LAST:MVDGetDisplay

	/**
	 * This method should exit the midlet.
	 */
	public void exitMIDlet() {// GEN-FIRST:MVDExitMidlet
		getDisplay().setCurrent(null);
		destroyApp(true);
		notifyDestroyed();
	}// GEN-LAST:MVDExitMidlet

	/**
	 * This method returns instance for textValue component and should be called
	 * instead of accessing textValue field directly.//GEN-BEGIN:MVDGetBegin3
	 * 
	 * @return Instance for textValue component
	 */
	public TextBox get_textValue() {
		if (textValue == null) {// GEN-END:MVDGetBegin3
			// Insert pre-init code here
			textValue = new TextBox("ScriptME - EcmaScript Interpreter", null,
					500, TextField.ANY);// GEN-BEGIN:MVDGetInit3
			textValue.addCommand(get_okCommand1());
			textValue.addCommand(get_exitCommand1());
			textValue.setCommandListener(this);// GEN-END:MVDGetInit3
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd3
		return textValue;
	}// GEN-END:MVDGetEnd3

	/**
	 * This method returns instance for okCommand1 component and should be
	 * called instead of accessing okCommand1 field
	 * directly.//GEN-BEGIN:MVDGetBegin4
	 * 
	 * @return Instance for okCommand1 component
	 */
	public Command get_okCommand1() {
		if (okCommand1 == null) {// GEN-END:MVDGetBegin4
			// Insert pre-init code here
			okCommand1 = new Command("Ok", Command.OK, 1);// GEN-LINE:MVDGetInit4
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd4
		return okCommand1;
	}// GEN-END:MVDGetEnd4

	/**
	 * This method returns instance for exitCommand1 component and should be
	 * called instead of accessing exitCommand1 field
	 * directly.//GEN-BEGIN:MVDGetBegin6
	 * 
	 * @return Instance for exitCommand1 component
	 */
	public Command get_exitCommand1() {
		if (exitCommand1 == null) {// GEN-END:MVDGetBegin6
			// Insert pre-init code here
			exitCommand1 = new Command("Exit", Command.EXIT, 1);// GEN-LINE:MVDGetInit6
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd6
		return exitCommand1;
	}// GEN-END:MVDGetEnd6

	/**
	 * This method returns instance for form1 component and should be called
	 * instead of accessing form1 field directly.//GEN-BEGIN:MVDGetBegin8
	 * 
	 * @return Instance for form1 component
	 */
	public Form get_form1() {
		if (form1 == null) {// GEN-END:MVDGetBegin8
			// Insert pre-init code here
			form1 = new Form("ScriptME - EcmaScript Interpreter",
					new Item[] { get_esSource() });// GEN-BEGIN:MVDGetInit8
			form1.addCommand(get_okCommand2());
			form1.addCommand(get_exitCommand2());
			form1.setCommandListener(this);// GEN-END:MVDGetInit8
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd8
		return form1;
	}// GEN-END:MVDGetEnd8

	/**
	 * This method returns instance for esSource component and should be called
	 * instead of accessing esSource field directly.//GEN-BEGIN:MVDGetBegin9
	 * 
	 * @return Instance for esSource component
	 */
	public TextField get_esSource() {
		if (esSource == null) {// GEN-END:MVDGetBegin9
			// Insert pre-init code here
			esSource = new TextField("Script: ", null, 500, TextField.ANY);// GEN-LINE:MVDGetInit9
			String script = "function go() {\n" + "   var i = 0;\n"
					+ "   var o = i;\n" + "   for (i=0; i<10;i++) { \n"
					+ "   }\n"
					+ "   var msg = 'Value of variable i before loop: '\n"
					+ "               + i + \n"
					+ "               '\\nValue of variable i after loop: '\n"
					+ "               + o;\n" + "   alert(msg);\n" + "}\n"
					+ "// Call function go()\n" + "go();";
			esSource.setString(script);
		}// GEN-BEGIN:MVDGetEnd9
		return esSource;
	}// GEN-END:MVDGetEnd9

	/**
	 * This method returns instance for okCommand2 component and should be
	 * called instead of accessing okCommand2 field
	 * directly.//GEN-BEGIN:MVDGetBegin10
	 * 
	 * @return Instance for okCommand2 component
	 */
	public Command get_okCommand2() {
		if (okCommand2 == null) {// GEN-END:MVDGetBegin10
			// Insert pre-init code here
			okCommand2 = new Command("Ok", Command.OK, 1);// GEN-LINE:MVDGetInit10
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd10
		return okCommand2;
	}// GEN-END:MVDGetEnd10

	/**
	 * This method returns instance for exitCommand2 component and should be
	 * called instead of accessing exitCommand2 field
	 * directly.//GEN-BEGIN:MVDGetBegin12
	 * 
	 * @return Instance for exitCommand2 component
	 */
	public Command get_exitCommand2() {
		if (exitCommand2 == null) {// GEN-END:MVDGetBegin12
			// Insert pre-init code here
			exitCommand2 = new Command("Exit", Command.EXIT, 1);// GEN-LINE:MVDGetInit12
			// Insert post-init code here
		}// GEN-BEGIN:MVDGetEnd12
		return exitCommand2;
	}// GEN-END:MVDGetEnd12

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	public void startApp() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	public void pauseApp() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	public void destroyApp(boolean unconditional) {
	}
}
